/**
 * Created with IntelliJ IDEA.
 * @author  ranhongmin--2018/4/6
 * Description:
 */
var wordId;
$(function () {

    createMain();
    createProperty();
    createInfo();


});


function createMain() {

    wordId = getUrlParam("wordId");

    $.ajax({
        type: "POST",
        url: "../../wordMainAction/getMainById.json",
        dataType: "json",
        data: {
            "wordId": wordId
        },
        success: function (data) {
            // alert(data);
            $('#append').empty();
            var n = data.data.obj;
            $('#append').append(" <article data-wordId=\"" + n.id + "\"  class=\"blog-main\">" +
                "<h3 class=\"am-article-title blog-title\">" +
                " <a href=\"#\">" + n.wordName + "</a>" +
                "</h3>" +
                "<div class=\"am-g blog-content\">" +
                " <div class=\"am-u-lg-7\">" + n.wordIntroduction + "</div>" +
                "<div class=\"am-u-lg-5\">" +
                "<p><img src=\"" + n.wordImg + "\"></p>" +
                " </div>" +
                "</div></article>" +
                "        <hr class=\"am-article-divider blog-hr\">");
        },
        error: function () {
            alert("获取失败，服务错误！！");
        }
    });
}


function createProperty() {

    $.ajax({
        type: "POST",
        url: "../../wordPropertyAction/getPropertyByWordId.json",
        dataType: "json",
        data: {
            "wordId": wordId
        },
        success: function (data) {
            // alert(data);
            $('#property_append').empty();
            $.each(data.data.list, function (i, n) {
                if (Number(i) != 0 && Number(i) % 2 === 0) {
                    $('#table_append2').append("  <tr>\n" +
                        "                            <td>" + n.propertyName + "</td>\n" +
                        "                            <td>" + n.propertyIntroduction + "</td>\n" +
                        "                        </tr>");
                } else {
                    $('#table_append1').append("  <tr>\n" +
                        "                            <td>" + n.propertyName + "</td>\n" +
                        "                            <td>" + n.propertyIntroduction + "</td>\n" +
                        "                        </tr>");
                }

            });

        },
        error: function () {
            alert("获取失败，服务错误！！");
        }
    });

}

function createInfo() {

    $.ajax({
        type: "POST",
        url: "../../wordInfoAction/getInfoListByWordId.json",
        dataType: "json",
        data: {
            "wordId": wordId
        },
        success: function (data) {
            // alert(data);
            $('#append_info').empty();
            $.each(data.data.list, function (i, n) {
                $('#append_info').append(" \n" +
                    "<article class=\"blog-main\" id=\"append\">\n" +
                    "   <header> <h3 class=\"am-article-title blog-title\">\n" +
                    "            <a href=\"\">" + n.infoTitle + "</a>\n" +
                    "            </h3></header>\n" +
                    "            " + n.infoDetails + "\n" +
                    "        </article>" +
                    "        <hr class=\"am-article-divider blog-hr\">\n");
            });
        },
        error: function () {
            alert("获取失败，服务错误！！");
        }
    });
}


/*
*
* 获取url中的字段值
*
* @param name
* @returns {*}
*/
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}